package test.sense;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.UUID;

public class StringUtils {
  /**
   * 判断字符串是否为空.
   * 
   * @param str
   *            源字符串.
   * @return bool.
   */
  public static boolean isEmpty(String str) {
    return str == null || str.length() == 0;
  }

  /**
   * 十六进制字符串转字节数组
   * 
   * @param hex.
   * @return byte[] .
   */
  public static byte[] hex2Bytes(String hex) {
    if (hex == null || hex.length() == 0) {
      return null;
    }

    int length = hex.length() / 2;
    byte[] result = new byte[length];
    for (int i = 0; i < length; i++) {
      result[i] = (byte) Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
    }
    return result;
  }

  /**
   * 字节数据转十六进制字符串
   * 
   * @param bytes.
   * @returnstr.
   */
  public static String bytes2Hex(byte[] bytes) {
    if (bytes == null || bytes.length == 0) {
      return null;
    }

    BigInteger bi = new BigInteger(1, bytes);
    String hex = bi.toString(16);
    int paddingLength = (bytes.length * 2) - hex.length();
    if (paddingLength > 0) {
      return String.format("%0" + paddingLength + "d", 0) + hex;
    } else {
      return hex;
    }
  }
  
  /**
  * @return str.
  */
  public static String getUniqeLowerCaseStringWithuuid() {
    String str = UUID.randomUUID().toString().replaceAll("-", "");
    return str.toLowerCase();
  }

  /**
   * @param url.
   * @return bool.
   */
  public static boolean checkUrl(String url) {
    if ( url == null || !url.contains("http") ) {
      return false;
    }
    return true;
  }

  /**
   * 
   * @param dataStr.
   * @param splitStr.
   * @param cls.
   * @return T list.
   */
  public static <T> List<T> str2List(String dataStr, String splitStr, Class<T> cls) {
    String[] ss = dataStr.split(splitStr);
    List<T> list = new ArrayList<>();
    for (String s : ss) {
      if (!isEmpty(s)) {
        String type = cls.getSimpleName();
        if ("Integer".equals(type)) {
          Integer i00 = Integer.parseInt(s);
          list.add(cls.cast(i00));
        }
        if ("Long".equals(type)) {
          Long i11 = Long.parseLong(s);
          list.add(cls.cast(i11));
        }
        if ("Double".equals(type)) {
          Double i22 = Double.parseDouble(s);
          list.add(cls.cast(i22));
        }
        if ("Boolean".equals(type)) {
          Boolean i33 = Boolean.parseBoolean(s);
          list.add(cls.cast(i33));
        }
        if ("String".equals(type)) {
          list.add(cls.cast(s));
        }

      }
    }
    return list;
  }

  /**
   * 
   * @param lists.
   * @return str.
   */
  public static String list2Str(List<?> lists) {
    StringBuilder sb = new StringBuilder();
    if (lists.size() > 0) {
      for (Object o : lists) {
        sb.append(o).append(",");
      }
      String res = sb.toString();
      if (res != null && res.length() > 0) {
        return res.substring(0, res.length() - 1);
      }
    }
    return null;
  }

  /**
   * 
   * @param map.
   * @return str.
   */
  public static String map2Str(Map<?, ?> map) {
    StringBuilder sb = new StringBuilder();
    if (!map.isEmpty()) {
      for (Entry<?, ?> e : map.entrySet()) {
        sb.append(e.getKey()).append(":").append(e.getValue()).append(",");
      }
      String res = sb.toString();
      if (res != null && res.length() > 0) {
        return res.substring(0, res.length() - 1);
      }
    }
    return null;
  }

  /**
   * 
   * @param dataStr.
   * @return map .
   */
  public static Map<String, String> str2Map(String dataStr) {
    Map<String, String> map = new HashMap<String, String>();
    String[] ss = dataStr.split(",");
    for (String s : ss) {
      if (!isEmpty(s)) {
        String[] kv = s.split(":");
        map.put(kv[0], kv[1]);
      }
    }
    return map;
  }

  /**
   * @info 从URL地址获得URI的方法
   * @param url.
   * @return String.
   */
  public static String getAction(String url) {
    int j11 = url.indexOf("//");
    int i11 = url.indexOf("/", j11 + 2);

    String action = url.substring(i11, url.length());
    int k11 = url.indexOf("?");
    if (k11 > 0) {
      action = url.substring(i11, k11);
    }
    return action;
  }

  /**
   * @info 从URL地址获得HOSTIPPORT
   * @param url.
   * @return String.
   */
  public static String getHttpIpPort(String url) {
    String action = getAction(url);
    int index = url.indexOf(action);
    return url.substring(0, index);
  }
  
  /**
   * @info MAP排序方法
   * @param url.
   * @return map .
   */
  public static Map<String, String> sortMap(Map<String, String> map) {
    Map<String, String> resMap = new TreeMap<String, String>(new Comparator<String>() {
      public int compare(String o1, String o2) {
        return o1.compareTo(o2);
      }
    });
    resMap.putAll(map);
    return resMap;
  }

  /**
   * @info URL带有QueryString信息进行提取
   * @param url .
   * @return map .
   * @throws Exception exec.
   */
  public static  Map<String, String> queryStr2Map(String url) throws Exception {
    if (url == null) {
      throw new Exception("url is not null");
    }

    if (!StringUtils.checkUrl(url)) {
      throw new Exception("url is not rightful");
    }

    Map<String, String> map = new HashMap<>();
    String queryStr = null;
    int i11 = url.indexOf("?");
    if (i11 > -1) {
      queryStr = url.substring(i11 + 1, url.length());
      if (queryStr != null && queryStr.length() > 0) {
        if (queryStr.contains("&")) {
          String[] ss = queryStr.split("&");
          for (String s : ss) {
            if (s.contains("=")) {
              String[] kvs = s.split("=");
              if (kvs != null && kvs.length == 2) {
                if (kvs[0] != null && kvs[1] != null) {
                  map.put(kvs[0], kvs[1]);
                }
              }
            }
          }
        } else {
          if (queryStr.contains("=")) {
            String[] kvs = queryStr.split("=");
            if (kvs != null && kvs.length == 2) {
              if (kvs[0] != null && kvs[1] != null) {
                map.put(kvs[0], kvs[1]);
              }
            }
          }
        }
      }
    }

    return map;
  }

}
